package com.groupwork.meeting.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.groupwork.meeting.entity.Role;
import com.groupwork.meeting.entity.User;
import com.groupwork.meeting.entity.UserRole;
import com.groupwork.meeting.service.IUserRoleService;
import com.groupwork.meeting.service.IUserService;
import com.groupwork.meeting.service.ex.InsertException;
import com.groupwork.meeting.service.ex.UserNameDoplicatedException;
import com.groupwork.meeting.util.JsonResult;
import com.sun.org.apache.xml.internal.serializer.SerializerBase;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 鱼凫
 * @version 1.0
 * Create by 2021/10/29 23:06
 * @Description //TODO
 */

@RestController
@RequestMapping("users")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    private IUserRoleService userRoleService;

    @RequestMapping("adduser")
    public JsonResult<Void> reg(User user) {
        userService.reg(user);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("login")
    public JsonResult<User> login(String username, String password, HttpSession session) {
        User data = userService.login(username, password);
        //登录成功后，将uid和username存入到HttpSession中
        session.setAttribute("uid", data.getUserId());
        session.setAttribute("username", data.getUserName());
        session.setAttribute("urname",data.getUrname());
        return new JsonResult<User>(OK, data);
    }

    @GetMapping("get_by_uid")
    public JsonResult<User> getByUid(HttpSession session) {
        // 从HttpSession对象中获取uid
        String uid = getUidFromSession(session);
        // 调用业务对象执行获取数据
        User data = userService.getByUid(uid);
        // 响应成功和数据
        return new JsonResult<User>(OK, data);
    }

    @GetMapping("get_by_uname")
    public JsonResult<User> getByUname(HttpSession session) {
        String uname = getUidFromSession(session);
        User data = userService.getByUid(uname);
        return new JsonResult<User>(OK, data);
    }

    @RequestMapping("{uname}/delete")
    public JsonResult<Void> delete(@PathVariable("uname") String uname){
        userService.deleteUser(uname);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("get_user_list")
    public JsonResult<List<User>> getUserList(){
        List<User> data = userService.getUserList();
        return new JsonResult<>(OK,data);
    }
}
